package thulearningcore;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.URLEncoder;

import thulearningcore.util.*;

/**
 * @author Nostalgia
 * 
 */
public class LoginManager {
	private String userid = "";
	private String userpass = "";
	private int accountType = ACCOUNT_UNKNOWN;

	private static final String baseURL =  LearningManager.getBaseAddress();//"https://learn.tsinghua.edu.cn";
	private static final String defaultURL = "/MultiLanguage/lesson/teacher/loginteacher.jsp";
	private static final String checkURL = "/MultiLanguage/lesson/teacher/mainteacher.jsp";

	public static final int SUCCESS = 0;
	public static final int NETWORK_ERROR = 1;
	public static final int USERNAME_PASSWORD_ERROR = 2;

	public static final int ACCOUNT_UNKNOWN = 0x10;
	public static final int ACCOUNT_TEACHER = 0x11;
	public static final int ACCOUNT_STUDENT = 0x12;
	public static final int ACCOUNT_TA = 0x13;

	/**
	 * @param username
	 *            User's username
	 * 
	 * @param password
	 *            User's password
	 */
	public LoginManager(String username, String password) {
		userid = username;
		userpass = password;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getUserpass() {
		return userpass;
	}

	public void setUserpass(String userpass) {
		this.userpass = userpass;
	}

	public void setAccountType(int type) {
		this.accountType = type;
	}
	
	public int getAccountType() {
		return this.accountType;
	}
	
	/**
	 * Try login
	 * 
	 * @return SUCCESS if login is succeeded,or errorcode if failed.
	 */
	public int login() {
		try {
			String url = baseURL + defaultURL + "?userid="
					+ URLEncoder.encode(userid, "utf-8") + "&userpass="
					+ URLEncoder.encode(userpass, "utf-8");
			String result = TLURLConnection.post(url);
			int br = result.contains("loginteacher_action.jsp") ? SUCCESS
					: USERNAME_PASSWORD_ERROR;
			if(br == SUCCESS) {
				String newResult = TLURLConnection.post(baseURL + checkURL);
				this.accountType = newResult.contains("mainstudent.jsp")?ACCOUNT_STUDENT:ACCOUNT_TEACHER;
			}
			return br;
		} catch (Exception e) {
			e.printStackTrace();
			return NETWORK_ERROR;
		}
	}

	public void writeData(DataOutputStream dos) throws IOException {
		dos.writeUTF(this.userid);
		dos.writeUTF(this.userpass);
	}

	public LoginManager readData(DataInputStream dis) throws IOException {
		this.userid = dis.readUTF();
		this.userpass = dis.readUTF();
		return this;
	}

	/*
	 * not used,all packaged to TLURLConnection
	 * 
	 * private String post(URL url) throws Exception{
	 * 
	 * HttpURLConnection httpc = (HttpURLConnection) url.openConnection();
	 * TLCookieManager.getInstance().addCookieFromURLConnection(httpc);
	 * 
	 * InputStreamReader reader = new InputStreamReader( httpc.getInputStream(),
	 * "utf-8"); BufferedReader breader = new BufferedReader(reader,8192);
	 * 
	 * StringBuilder sb = new StringBuilder(4096); CharBuffer charBuffer =
	 * CharBuffer.allocate(4096); while (breader.read(charBuffer) != -1) { if
	 * (charBuffer.length() == 0) { charBuffer.flip(); sb.append(charBuffer); }
	 * } charBuffer.flip(); sb.append(charBuffer); String result =
	 * sb.toString();
	 * 
	 * breader.close(); reader.close();
	 * 
	 * return result; }
	 */
}
